<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>AVLTreeST.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="AVLTreeST code in Java">
<meta NAME="TITLE" CONTENT="AVLTreeST code in Java">
<meta NAME="KEYWORDS" CONTENT="AVLTreeST,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>AVLTreeST.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "AVLTreeST.java">AVLTreeST.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac AVLTreeST.java</span>
<span class="comment"> *  Execution:    java AVLTreeST &lt; input.txt</span>
<span class="comment"> *  Dependencies: StdIn.java StdOut.java  </span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/33balanced/tinyST.txt</span><span class="comment">  </span>
<span class="comment"> *    </span>
<span class="comment"> *  A symbol table implemented using an AVL tree.</span>
<span class="comment"> *</span>
<span class="comment"> *  % more tinyST.txt</span>
<span class="comment"> *  S E A R C H E X A M P L E</span>
<span class="comment"> *  </span>
<span class="comment"> *  % java AVLTreeST &lt; tinyST.txt</span>
<span class="comment"> *  A 8</span>
<span class="comment"> *  C 4</span>
<span class="comment"> *  E 12</span>
<span class="comment"> *  H 5</span>
<span class="comment"> *  L 11</span>
<span class="comment"> *  M 9</span>
<span class="comment"> *  P 10</span>
<span class="comment"> *  R 3</span>
<span class="comment"> *  S 0</span>
<span class="comment"> *  X 7</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">NoSuchElementException</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;code&gt;</span><span class="comment">AVLTreeST</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> class represents an ordered symbol table of</span>
<span class="comment"> *  generic key-value pairs. It supports the usual </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">contains</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">delete</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">size</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">is-empty</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  methods. It also provides ordered methods for finding the </span><span class="keyword">&lt;em&gt;</span><span class="comment">minimum</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">maximum</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">floor</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">ceiling</span><span class="keyword">&lt;/em&gt;</span><span class="comment">. It also provides a</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">keys</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> method for iterating over all of the keys. A symbol table</span>
<span class="comment"> *  implements the </span><span class="keyword">&lt;em&gt;</span><span class="comment">associative array</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> abstraction: when associating a</span>
<span class="comment"> *  value with a key that is already in the symbol table, the convention is to</span>
<span class="comment"> *  replace the old value with the new value. Unlike {</span><span class="type">@link</span><span class="comment"> java.util.Map}, this</span>
<span class="comment"> *  class uses the convention that values cannot be </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment"> *  </span><span class="preproc">&amp;mdash;</span><span class="comment">setting the value associated with a key to </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is</span>
<span class="comment"> *  equivalent to deleting the key from the symbol table.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This symbol table implementation uses internally an</span>
<span class="comment"> *  </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"https://en.wikipedia.org/wiki/AVL_tree"</span><span class="keyword">&gt;</span><span class="comment"> AVL tree </span><span class="keyword">&lt;/a&gt;</span><span class="comment"> (Georgy</span>
<span class="comment"> *  Adelson-Velsky and Evgenii Landis' tree) which is a self-balancing BST.</span>
<span class="comment"> *  In an AVL tree, the heights of the two child subtrees of any</span>
<span class="comment"> *  node differ by at most one; if at any time they differ by more than one,</span>
<span class="comment"> *  rebalancing is done to restore this property.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation requires that the key type implements the</span>
<span class="comment"> *  </span><span class="keyword">&lt;code&gt;</span><span class="comment">Comparable</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> interface and calls the </span><span class="keyword">&lt;code&gt;</span><span class="comment">compareTo()</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> and</span>
<span class="comment"> *  method to compare two keys. It does not call either </span><span class="keyword">&lt;code&gt;</span><span class="comment">equals()</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> or</span>
<span class="comment"> *  </span><span class="keyword">&lt;code&gt;</span><span class="comment">hashCode()</span><span class="keyword">&lt;/code&gt;</span><span class="comment">. The </span><span class="keyword">&lt;em&gt;</span><span class="comment">put</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">get</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">contains</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">delete</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">minimum</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">maximum</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">ceiling</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">floor</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operations each take logarithmic time in the worst case. The</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">size</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">is-empty</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> operations take constant time.</span>
<span class="comment"> *  Construction also takes constant time.</span>
<span class="comment"> * </span>
<span class="comment"> *  For other implementations of the same API, see {</span><span class="type">@link</span><span class="comment"> ST}, {</span><span class="type">@link</span><span class="comment"> BinarySearchST},</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> SequentialSearchST}, {</span><span class="type">@link</span><span class="comment"> BST}, {</span><span class="type">@link</span><span class="comment"> RedBlackBST},</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> SeparateChainingHashST}, and {</span><span class="type">@link</span><span class="comment"> LinearProbingHashST}.</span>
<span class="comment"> * </span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Marcelo Silva</span>
<span class="comment"> */</span>

<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">AVLTreeST</span><span class="symbol">&lt;</span><span class="normal">Key </span><span class="usertype">extends</span><span class="normal"> Comparable</span><span class="symbol">&lt;</span><span class="normal">Key</span><span class="symbol">&gt;,</span><span class="normal"> Value</span><span class="symbol">&gt;</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * The root node.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> root</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This class represents an inner node of the AVL tree.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Node</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// the key</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// the associated value</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> h</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// height of the subtree</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// number of nodes in subtree</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> left</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// left subtree</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> right</span><span class="symbol">;</span><span class="normal"> </span><span class="comment">// right subtree</span>

<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> h</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> N</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">key </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">val </span><span class="symbol">=</span><span class="normal"> val</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> N</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> h</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an empty symbol table.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">AVLTreeST</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if the symbol table is empty.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the symbol table is empty.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isEmpty</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> root </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number key-value pairs in the symbol table.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number key-value pairs in the symbol table</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of nodes in the subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of nodes in the subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">N</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the height of the internal AVL tree. It is assumed that the</span>
<span class="comment">     * height of an empty tree is -1 and the height of a tree with just one node</span>
<span class="comment">     * is 0.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the height of the internal AVL tree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">height</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the height of the subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the height of the subtree.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">h</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the value associated with the given key.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the value associated with the given key if the key is in the</span>
<span class="comment">     *         symbol table and </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the key is not in the</span>
<span class="comment">     *         symbol table</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument to get() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">val</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns value associated with the given key in the subtree or</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if no such key.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> value associated with the given key in the subtree or</span>
<span class="comment">     *         </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if no such key</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmp </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if the symbol table contains the given key.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the symbol table contains </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     *         and </span><span class="keyword">&lt;code&gt;</span><span class="comment">false</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">contains</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">get</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Inserts the specified key-value pair into the symbol table, overwriting</span>
<span class="comment">     * the old value with the new value if the symbol table already contains the</span>
<span class="comment">     * specified key. Deletes the specified key (and its associated value) from</span>
<span class="comment">     * this symbol table if the specified value is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> val the value</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"first argument to put() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">val </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">delete</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        root </span><span class="symbol">=</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// assert check();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Inserts the key-value pair in the subtree. It overrides the old value</span>
<span class="comment">     * with the new value if the symbol table already contains the specified key</span>
<span class="comment">     * and deletes the specified key (and its associated value) from this symbol</span>
<span class="comment">     * table if the specified value is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> val the value</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Value</span><span class="normal"> val</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Node</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmp </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> </span><span class="function">put</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> val</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            x</span><span class="symbol">.</span><span class="normal">val </span><span class="symbol">=</span><span class="normal"> val</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">balance</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Restores the AVL tree property of the subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the subtree with restored AVL property</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">balance</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">balanceFactor</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">balanceFactor</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> </span><span class="function">rotateRight</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">rotateLeft</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">balanceFactor</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">balanceFactor</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> </span><span class="function">rotateLeft</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">rotateRight</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the balance factor of the subtree. The balance factor is defined</span>
<span class="comment">     * as the difference in height of the left subtree and right subtree, in</span>
<span class="comment">     * this order. Therefore, a subtree with a balance factor of -1, 0 or 1 has</span>
<span class="comment">     * the AVL property since the heights of the two child subtrees differ by at</span>
<span class="comment">     * most one.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the balance factor of the subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">balanceFactor</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rotates the given subtree to the right.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the right rotated subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">rotateRight</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">        y</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        y</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">N</span><span class="symbol">;</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        y</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Rotates the given subtree to the left.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the left rotated subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">rotateLeft</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">        y</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        y</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">N</span><span class="symbol">;</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        y</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Removes the specified key and its associated value from the symbol table</span>
<span class="comment">     * (if the key is in the symbol table).</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">delete</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument to delete() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        root </span><span class="symbol">=</span><span class="normal"> </span><span class="function">delete</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// assert check();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Removes the specified key and its associated value from the given</span>
<span class="comment">     * subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the updated subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">delete</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmp </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> </span><span class="function">delete</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> </span><span class="function">delete</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">Node</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">                x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">min</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">                x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> </span><span class="function">deleteMin</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">                x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">balance</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Removes the smallest key and associated value from the symbol table.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NoSuchElementException if the symbol table is empty</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">deleteMin</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NoSuchElementException</span><span class="symbol">(</span><span class="string">"called deleteMin() with empty symbol table"</span><span class="symbol">);</span>
<span class="normal">        root </span><span class="symbol">=</span><span class="normal"> </span><span class="function">deleteMin</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Removes the smallest key and associated value from the given subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the updated subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">deleteMin</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">;</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">=</span><span class="normal"> </span><span class="function">deleteMin</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">balance</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Removes the largest key and associated value from the symbol table.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NoSuchElementException if the symbol table is empty</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">deleteMax</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NoSuchElementException</span><span class="symbol">(</span><span class="string">"called deleteMax() with empty symbol table"</span><span class="symbol">);</span>
<span class="normal">        root </span><span class="symbol">=</span><span class="normal"> </span><span class="function">deleteMax</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Removes the largest key and associated value from the given subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the updated subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">deleteMax</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">;</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">=</span><span class="normal"> </span><span class="function">deleteMax</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        x</span><span class="symbol">.</span><span class="normal">h </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">max</span><span class="symbol">(</span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">),</span><span class="normal"> </span><span class="function">height</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">));</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">balance</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the smallest key in the symbol table.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the smallest key in the symbol table</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NoSuchElementException if the symbol table is empty</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> </span><span class="function">min</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NoSuchElementException</span><span class="symbol">(</span><span class="string">"called min() with empty symbol table"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">min</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">).</span><span class="normal">key</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the node with the smallest key in the subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the node with the smallest key in the subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">min</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">min</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the largest key in the symbol table.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the largest key in the symbol table</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NoSuchElementException if the symbol table is empty</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> </span><span class="function">max</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NoSuchElementException</span><span class="symbol">(</span><span class="string">"called max() with empty symbol table"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">max</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">).</span><span class="normal">key</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the node with the largest key in the subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the node with the largest key in the subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">max</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">max</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the largest key in the symbol table less than or equal to</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the largest key in the symbol table less than or equal to</span>
<span class="comment">     *         </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NoSuchElementException if the symbol table is empty</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> </span><span class="function">floor</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument to floor() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NoSuchElementException</span><span class="symbol">(</span><span class="string">"called floor() with empty symbol table"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">floor</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the node in the subtree with the largest key less than or equal</span>
<span class="comment">     * to the given key.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the node in the subtree with the largest key less than or equal</span>
<span class="comment">     *         to the given key</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">floor</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmp </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">floor</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> </span><span class="function">floor</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">y </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the smallest key in the symbol table greater than or equal to</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the smallest key in the symbol table greater than or equal to</span>
<span class="comment">     *         </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NoSuchElementException if the symbol table is empty</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> </span><span class="function">ceiling</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument to ceiling() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NoSuchElementException</span><span class="symbol">(</span><span class="string">"called ceiling() with empty symbol table"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">ceiling</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the node in the subtree with the smallest key greater than or</span>
<span class="comment">     * equal to the given key.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the node in the subtree with the smallest key greater than or</span>
<span class="comment">     *         equal to the given key</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">ceiling</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmp </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">ceiling</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> </span><span class="function">ceiling</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">y </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> y</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the kth smallest key in the symbol table.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> k the order statistic</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the kth smallest key in the symbol table</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless </span><span class="keyword">&lt;code&gt;</span><span class="comment">k</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is between 0 and</span>
<span class="comment">     *             </span><span class="keyword">&lt;code&gt;</span><span class="comment"> size() -1 </span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> </span><span class="function">select</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> k</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">k </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> k </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="function">size</span><span class="symbol">())</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"k is not in range 0-"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="symbol">(</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">));</span>
<span class="normal">        </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> </span><span class="function">select</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> k</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the node with key the kth smallest key in the subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> k the kth smallest key in the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the node with key the kth smallest key in the subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> </span><span class="function">select</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> k</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> t </span><span class="symbol">=</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">t </span><span class="symbol">&gt;</span><span class="normal"> k</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">select</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> k</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">t </span><span class="symbol">&lt;</span><span class="normal"> k</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">select</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> k </span><span class="symbol">-</span><span class="normal"> t </span><span class="symbol">-</span><span class="normal"> </span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of keys in the symbol table strictly less than</span>
<span class="comment">     * </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of keys in the symbol table strictly less than</span>
<span class="comment">     *         </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if </span><span class="keyword">&lt;code&gt;</span><span class="comment">key</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"argument to rank() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> root</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of keys in the subtree less than key.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> key the key</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of keys in the subtree less than key</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmp </span><span class="symbol">=</span><span class="normal"> key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmp </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns all keys in the symbol table.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all keys in the symbol table</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Key&gt;</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">keysInOrder</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns all keys in the symbol table following an in-order traversal.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all keys in the symbol table following an in-order traversal</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Key&gt;</span><span class="normal"> </span><span class="function">keysInOrder</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Queue&lt;Key&gt;</span><span class="normal"> queue </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Key</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="function">keysInOrder</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> queue</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Adds the keys in the subtree to queue following an in-order traversal.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> queue the queue</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keysInOrder</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Queue&lt;Key&gt;</span><span class="normal"> queue</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">keysInOrder</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">);</span>
<span class="normal">        queue</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">keysInOrder</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns all keys in the symbol table following a level-order traversal.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all keys in the symbol table following a level-order traversal.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Key&gt;</span><span class="normal"> </span><span class="function">keysLevelOrder</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Queue&lt;Key&gt;</span><span class="normal"> queue </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Key</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">Queue&lt;Node&gt;</span><span class="normal"> queue2 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Node</span><span class="symbol">&gt;();</span>
<span class="normal">            queue2</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">queue2</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">Node</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> queue2</span><span class="symbol">.</span><span class="function">dequeue</span><span class="symbol">();</span>
<span class="normal">                queue</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    queue2</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    queue2</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> queue</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns all keys in the symbol table in the given range.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> lo the lowest key</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> hi the highest key</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> all keys in the symbol table between </span><span class="keyword">&lt;code&gt;</span><span class="comment">lo</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> (inclusive)</span>
<span class="comment">     *         and </span><span class="keyword">&lt;code&gt;</span><span class="comment">hi</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> (exclusive)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if either </span><span class="keyword">&lt;code&gt;</span><span class="comment">lo</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> or </span><span class="keyword">&lt;code&gt;</span><span class="comment">hi</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     *             is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Key&gt;</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> hi</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">lo </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"first argument to keys() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">hi </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"second argument to keys() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Queue&lt;Key&gt;</span><span class="normal"> queue </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Key</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="function">keys</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">,</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> hi</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> queue</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Adds the keys between </span><span class="keyword">&lt;code&gt;</span><span class="comment">lo</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> and </span><span class="keyword">&lt;code&gt;</span><span class="comment">hi</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> in the subtree</span>
<span class="comment">     * to the </span><span class="keyword">&lt;code&gt;</span><span class="comment">queue</span><span class="keyword">&lt;/code&gt;</span><span class="comment">.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> queue the queue</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> lo the lowest key</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> hi the highest key</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Queue&lt;Key&gt;</span><span class="normal"> queue</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> hi</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmplo </span><span class="symbol">=</span><span class="normal"> lo</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> cmphi </span><span class="symbol">=</span><span class="normal"> hi</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmplo </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">,</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> hi</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmplo </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> cmphi </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> queue</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">cmphi </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> queue</span><span class="symbol">,</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> hi</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of keys in the symbol table in the given range.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of keys in the symbol table between </span><span class="keyword">&lt;code&gt;</span><span class="comment">lo</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     *         (inclusive) and </span><span class="keyword">&lt;code&gt;</span><span class="comment">hi</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> (exclusive)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NullPointerException if either </span><span class="keyword">&lt;code&gt;</span><span class="comment">lo</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> or </span><span class="keyword">&lt;code&gt;</span><span class="comment">hi</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     *             is </span><span class="keyword">&lt;code&gt;</span><span class="comment">null</span><span class="keyword">&lt;/code&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> lo</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> hi</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">lo </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"first argument to size() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">hi </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">(</span><span class="string">"second argument to size() is null"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">lo</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">hi</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">hi</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="normal">hi</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="normal">lo</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="normal">hi</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">-</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="normal">lo</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if the AVL tree invariants are fine.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the AVL tree invariants are fine</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">check</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">isBST</span><span class="symbol">())</span><span class="normal"> StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Symmetric order not consistent"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">isAVL</span><span class="symbol">())</span><span class="normal"> StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"AVL property not consistent"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">isSizeConsistent</span><span class="symbol">())</span><span class="normal"> StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Subtree counts not consistent"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">isRankConsistent</span><span class="symbol">())</span><span class="normal"> StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Ranks not consistent"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">isBST</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">isAVL</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">isSizeConsistent</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">isRankConsistent</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if AVL property is consistent.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if AVL property is consistent.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isAVL</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">isAVL</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if AVL property is consistent in the subtree.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if AVL property is consistent in the subtree</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isAVL</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> bf </span><span class="symbol">=</span><span class="normal"> </span><span class="function">balanceFactor</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">bf </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> bf </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">isAVL</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">isAVL</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if the symmetric order is consistent.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the symmetric order is consistent</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isBST</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">isBST</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if the tree rooted at x is a BST with all keys strictly between</span>
<span class="comment">     * min and max (if min or max is null, treat as empty constraint) Credit:</span>
<span class="comment">     * Bob Dondero's elegant solution</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> min the minimum key in subtree</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> max the maximum key in subtree</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if if the symmetric order is consistent</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isBST</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> min</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Key</span><span class="normal"> max</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">min </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">min</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">max </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="normal">max</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">isBST</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">,</span><span class="normal"> min</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">isBST</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">,</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> max</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if size is consistent.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if size is consistent</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isSizeConsistent</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">isSizeConsistent</span><span class="symbol">(</span><span class="normal">root</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if the size of the subtree is consistent.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if the size of the subtree is consistent</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isSizeConsistent</span><span class="symbol">(</span><span class="usertype">Node</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">N </span><span class="symbol">!=</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">size</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">isSizeConsistent</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">left</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="function">isSizeConsistent</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">.</span><span class="normal">right</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Checks if rank is consistent.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;code&gt;</span><span class="comment">true</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> if rank is consistent</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isRankConsistent</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="function">size</span><span class="symbol">();</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">!=</span><span class="normal"> </span><span class="function">rank</span><span class="symbol">(</span><span class="function">select</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Key</span><span class="normal"> key </span><span class="symbol">:</span><span class="normal"> </span><span class="function">keys</span><span class="symbol">())</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">.</span><span class="function">compareTo</span><span class="symbol">(</span><span class="function">select</span><span class="symbol">(</span><span class="function">rank</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">)))</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;code&gt;</span><span class="comment">AVLTreeST</span><span class="keyword">&lt;/code&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">AVLTreeST&lt;String, Integer&gt;</span><span class="normal"> st </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> AVLTreeST</span><span class="symbol">&lt;</span><span class="normal">String</span><span class="symbol">,</span><span class="normal"> Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">StdIn</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">();</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">String</span><span class="normal"> key </span><span class="symbol">=</span><span class="normal"> StdIn</span><span class="symbol">.</span><span class="function">readString</span><span class="symbol">();</span>
<span class="normal">            st</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">key</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> s </span><span class="symbol">:</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">keys</span><span class="symbol">())</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> st</span><span class="symbol">.</span><span class="function">get</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">));</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
